﻿using UnityEngine;
using System.Collections;

public class Moviment : MonoBehaviour {
	private Vector3 speed = new Vector3(25, 0, 0);
	
	public int deaths;

	public GUIText mortes;

	bool jump = true;

	public Texture2D[] minhasTexturas = new Texture2D[4];

	// Use this for initialization
	void Start () 
	{
	
	}

		
	// Update is called once per frame
	void Update () 
	{
		//"gravidade"
		rigidbody.AddForce (0,-250,0);

		//se caiu, morreu.
		if(transform.position.y < -20)
		{
			Application.LoadLevel("GAME");
		}


		if(Input.GetKey("d"))
		{
			rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
			renderer.material.mainTexture = minhasTexturas[0];
		}
		
		if(Input.GetKey("a"))
		{
			rigidbody.MovePosition(rigidbody.position - speed * Time.deltaTime);
			renderer.material.mainTexture = minhasTexturas[1];
		}

		if(jump == true)
		{
			if(Input.GetKeyDown("w"))
			{
				rigidbody.AddForce (0,4000,0);
				jump = false;
			}
		}

		if((jump == false) & renderer.material.mainTexture == minhasTexturas[0])
		{
			renderer.material.mainTexture = minhasTexturas[2];
		}
		if((jump == false) & renderer.material.mainTexture == minhasTexturas[1])
		{
			renderer.material.mainTexture = minhasTexturas[3];
		}
		if((jump == true) & renderer.material.mainTexture == minhasTexturas[2])
		{
			renderer.material.mainTexture = minhasTexturas[0];
		}
		if((jump == true) & renderer.material.mainTexture == minhasTexturas[3])
		{
			renderer.material.mainTexture = minhasTexturas[1];
		}

	}

	void OnCollisionEnter(Collision collision)
	{		
		if(collision.gameObject.tag == "floor")
		{
			jump=true;

		}

		if(collision.gameObject.tag == "castle")
		{
			jump=false;
			mortes.text="Total deaths: "+deaths;
			transform.position = new Vector3 (294,0,0);


		}


		if(collision.gameObject.tag == "fire" || collision.gameObject.tag == "block" || collision.gameObject.tag == "shell" || collision.gameObject.tag == "rock")
		{
			transform.position = new Vector3(0,0,0);
			deaths++;



		}
	}
}
